package com.juguo.charginganimation.View.gallery;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.android.volley.VolleyError;
import com.chad.library.adapter.base.module.BaseLoadMoreModule;
import com.google.gson.Gson;
import com.juguo.charginganimation.Bean.FavoritesListBean;
import com.juguo.charginganimation.Bean.PhotoBean;
import com.juguo.charginganimation.Data.remote.RetrofitManager;
import com.juguo.charginganimation.repository.PixabayService;
import com.juguo.charginganimation.repository.VolleySingleton;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class GalleryViewModel extends AndroidViewModel {

    private static final String TAG = "cimoGallery";

    private PixabayService service = RetrofitManager.getInstance().getRetrofit().create(PixabayService.class);
    private CompositeDisposable mDisposable = new CompositeDisposable();

    //当前页数初始化
    private int currentPage = 1;

    public int getCurrentPage() {
        return currentPage;
    }


    //总页数初始化
    private int totalPage = 1;

    public int getTotalPage() {
        return totalPage;
    }

    //是否已经没数据
    static boolean ISEND = false;

    /**
     * 数据源
     */
    MutableLiveData<List<PhotoBean.Photos>> photos = new MutableLiveData<>();

    public MutableLiveData<List<FavoritesListBean.Favorites>> favorites = new MutableLiveData<>();

    public GalleryViewModel(@NonNull Application application) {
        super(application);
    }

    /**
     * 查询条件
     */
    public void favoritesList(){
        currentPage = 1;
        totalPage = 1;
        ISEND = false;
        Map<String, Object> map = new HashMap<>();
        map.put("order", "desc");
        map.put("pageNum", 1);
        map.put("pageSize", 10);
        map.put("sort","add_time");
        //{"param":{map}}
        Map<String, Object> param = new HashMap<>();
        param.put("param", map);
        RequestBody body = RequestBody.create(MediaType.get("application/json; charset=utf-8"), new Gson().toJson(param));
        mDisposable.add(
                service.favoritesListBean(body)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Consumer<FavoritesListBean>() {
                            @Override
                            public void accept(FavoritesListBean favoritesListBean) throws Exception {
                                //刷新的请求，用postValue()
                                Log.d(TAG, "loadMore: " + favoritesListBean);
                                favorites.postValue(favoritesListBean.getFavorites());
                            }
                        }, throwable -> Log.d(TAG, "loadMore: " + throwable)));
    }

    void resetQuery() {

        currentPage = 1;
        totalPage = 1;
        ISEND = false;

        Map<String, Object> map = new HashMap<>();
        map.put("order", "desc");
        map.put("pageNum", 1);
        map.put("pageSize", 10);
        map.put("sort","add_time");
        map.put("type",237);
        //{"param":{map}}
        Map<String, Object> param = new HashMap<>();
        param.put("param", map);
        RequestBody body = RequestBody.create(MediaType.get("application/json; charset=utf-8"), new Gson().toJson(param));
        mDisposable.add(
        service.getList(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(photoBean -> {
                    //刷新的请求，用postValue()
                    photos.postValue(photoBean.getPhotolist());
                }, throwable -> Log.d(TAG, "loadMore: " + throwable)));
    }

    void popular(){
        SharedPreferences sp = getApplication().getSharedPreferences("sp", Context.MODE_PRIVATE);
        String result_token = sp.getString("result_token", "");

        Map<String, Object> map = new HashMap<>();
        map.put("order", "desc");
        map.put("pageNum", 1);
        map.put("pageSize", 10);
        map.put("sort","add_time");
        map.put("type",238);
        //{"param":{map}}
        Map<String, Object> param = new HashMap<>();
        param.put("param", map);
        RequestBody body = RequestBody.create(MediaType.get("application/json; charset=utf-8"), new Gson().toJson(param));
        mDisposable.add(
                service.getList(body)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(photoBean -> {
                            //刷新的请求，用postValue()
                            photos.postValue(photoBean.getPhotolist());
                        }, throwable -> {
                            Log.d(TAG, "loadMore: " + throwable);
                        }));
    }

    void mcoll(){

        Map<String, Object> map = new HashMap<>();
        map.put("order", "desc");
        map.put("pageNum", 1);
        map.put("pageSize", 10);
        map.put("sort","add_time");
        map.put("type",239);
        //{"param":{map}}
        Map<String, Object> param = new HashMap<>();
        param.put("param", map);
        RequestBody body = RequestBody.create(MediaType.get("application/json; charset=utf-8"), new Gson().toJson(param));
        mDisposable.add(
                service.getList(body)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(photoBean -> {
                            //刷新的请求，用postValue()
                            photos.postValue(photoBean.getPhotolist());
                        }, throwable -> {
                            Log.d(TAG, "loadMore: " + throwable);
                        }));
    }

    void menudancing(){
        Map<String, Object> map = new HashMap<>();
        map.put("order", "desc");
        map.put("pageNum", 1);
        map.put("pageSize", 10);
        map.put("sort","add_time");
        map.put("type",240);
        //{"param":{map}}
        Map<String, Object> param = new HashMap<>();
        param.put("param", map);
        RequestBody body = RequestBody.create(MediaType.get("application/json; charset=utf-8"), new Gson().toJson(param));
        mDisposable.add(
                service.getList(body)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(photoBean -> {
                            //刷新的请求，用postValue()
                            photos.postValue(photoBean.getPhotolist());
                        }, throwable -> {
                            Log.d(TAG, "loadMore: " + throwable);
                        }));
    }

    void menuanime(){
        Map<String, Object> map = new HashMap<>();
        map.put("order", "desc");
        map.put("pageNum", 1);
        map.put("pageSize", 10);
        map.put("sort","add_time");
        map.put("type",241);
        //{"param":{map}}
        Map<String, Object> param = new HashMap<>();
        param.put("param", map);
        RequestBody body = RequestBody.create(MediaType.get("application/json; charset=utf-8"), new Gson().toJson(param));
        mDisposable.add(
                service.getList(body)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(photoBean -> {
                            //刷新的请求，用postValue()
                            photos.postValue(photoBean.getPhotolist());
                        }, throwable -> {
                            Log.d(TAG, "loadMore: " + throwable);
                        }));
    }

    void menuchristmas(){
        Map<String, Object> map = new HashMap<>();
        map.put("order", "desc");
        map.put("pageNum", 1);
        map.put("pageSize", 10);
        map.put("sort","add_time");
        map.put("type",242);
        //{"param":{map}}
        Map<String, Object> param = new HashMap<>();
        param.put("param", map);
        RequestBody body = RequestBody.create(MediaType.get("application/json; charset=utf-8"), new Gson().toJson(param));
        mDisposable.add(
                service.getList(body)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(photoBean -> {
                            //刷新的请求，用postValue()
                            photos.postValue(photoBean.getPhotolist());
                        }, throwable -> {
                            Log.d(TAG, "loadMore: " + throwable);
                        }));
    }

    void menurock(){
        Map<String, Object> map = new HashMap<>();
        map.put("order", "desc");
        map.put("pageNum", 1);
        map.put("pageSize", 10);
        map.put("sort","add_time");
        map.put("type",243);
        //{"param":{map}}
        Map<String, Object> param = new HashMap<>();
        param.put("param", map);
        RequestBody body = RequestBody.create(MediaType.get("application/json; charset=utf-8"), new Gson().toJson(param));
        mDisposable.add(
                service.getList(body)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(photoBean -> {
                            //刷新的请求，用postValue()
                            photos.postValue(photoBean.getPhotolist());
                        }, throwable -> {
                            Log.d(TAG, "loadMore: " + throwable);
                        }));
    }



    /**
     * ViewModel的生命周期
     */
    @Override
    protected void onCleared() {
        super.onCleared();
        mDisposable.dispose();
    }
}
